package com.example.smartcity_poverty.ui.AllService.SmartBus.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_smart_bus_three.*
import kotlinx.coroutines.Dispatchers

/**
 * 第三步页面：显示起点与重点名称
 */
class SmartBusThreeActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt() }
    private val time by lazy { intent.getStringExtra("time").toString() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_smart_bus_three)
        title = "乘客信息"
        Util.addActivity(this)

        coroutine(Dispatchers.IO) {
            val result = api.getBusPathDataIls(id!!)
            if (result.code == 200) {
                runOnUiThread {
                    result.data?.let {
                        busThreeStartTv.text = it.first
                        busThreeEndTv.text = it.end
                    }
                }
            } else runOnUiThread { result.msg?.show() }
        }

        busThreeOk.setOnClickListener {
            if (busThreeTv1.text.toString().isNotEmpty() ||
                busThreeTv2.text.toString().isNotEmpty() ||
                busThreeTv3.text.toString().isNotEmpty() ||
                busThreeTv4.text.toString().isNotEmpty()
            ) {
                startActivity(Intent(this, SmartBusFourActivity::class.java).apply {
                    putExtra("id", id.toString())
                    putExtra("time", time.toString())
                    putExtra("tv1", busThreeTv1.text.toString())
                    putExtra("tv2", busThreeTv2.text.toString())
                    putExtra("tv3", busThreeTv3.text.toString())
                    putExtra("tv4", busThreeTv4.text.toString())
                })
            }else "输入信息不能为空".show()
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}